package com.pr.makfilm.user.pages.school.list.cmp;

import com.pr.makfilm.core.ajax.ConfirmationCallListener;
import com.pr.makfilm.core.cmp.button.BootstrapLink;
import com.pr.makfilm.core.cmp.notify.Notification;
import com.pr.makfilm.core.cmp.table.AjaxDataTable;
import com.pr.makfilm.core.cmp.table.toolbar.FilterToolbar;
import com.pr.makfilm.domain.District;
import com.pr.makfilm.domain.filter.OpenSearchDescription;
import com.pr.makfilm.service.IDistrictService;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * Created with IntelliJ IDEA.
 * User: pawel
 * Date: 08.12.13
 * Time: 15:10
 * To change this template use File | Settings | File Templates.
 */
public class DistrictFilterToolbar extends FilterToolbar<District>
{
   @SpringBean
   private IDistrictService districtService;

   private DistrictWindow window;

   private AjaxLink<Void> newDistrict;

   private BootstrapLink editDistrict;

   private BootstrapLink deleteDistrict;

   public DistrictFilterToolbar(AjaxDataTable<District> table, IModel<District> model)
   {
      super(table);
      setDefaultModel(model);
      setOutputMarkupId(true);
   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      window = new DistrictWindow("window")
      {
         @Override
         protected void onAfterSubmit(AjaxRequestTarget target)
         {
            refreshTableView(target);
         }
      };
      add(window);
      newDistrict = new AjaxLink<Void>("new")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            window.resetState(true);
            window.show(target);
         }
      };
      add(newDistrict);
      editDistrict = new BootstrapLink("edit")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            window.resetState(false);
            window.setDistrict(getSelected());
            window.show(target);
         }

         @Override
         protected void onConfigure()
         {
            setEnabled(getSelected() != null);
         }
      };
      add(editDistrict);

      deleteDistrict = new BootstrapLink("delete")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
//            List<School> schools = schoolService.findByType(getSelected());
//            if (CollectionUtils.isEmpty(schools))
//            {
//               schoolTypeService.delete(getSelected());
////               selected = null;
//               Notification.success(getString("type.successfully.deleted"));
//               refreshTableView(target);
//            }
//            else
//            {
//               Notification.error(getString("type.already.in.use"));
//            }
         }

         @Override
         protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
         {
            super.updateAjaxAttributes(attributes);
            attributes.getAjaxCallListeners().add(new ConfirmationCallListener(getString("district.delete.confirm")));
         }

         @Override
         protected void onConfigure()
         {
            setEnabled(false);
//            setEnabled(getSelected() != null);
         }
      };
      add(deleteDistrict);
   }

   @Override
   public void registerFilters(DataTable<?, ?> table, OpenSearchDescription<District> osd)
   {
      addFilter(new TextField<String>("name", new PropertyModel<String>(osd, "filter.name")), getString("name"));
   }

   private District getSelected()
   {
      return (District) getDefaultModelObject();
   }

   private void refreshTableView(AjaxRequestTarget target)
   {
      target.add(table);
      target.add(editDistrict);
      target.add(deleteDistrict);
   }
}
